home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj007.zip / BLOOM.ZIP / GRPHDEMO.C next >
Text File  |  1992-07-24  |  14KB  |  486 lines

  1. // grphdemo.c   GRAPHS dialog box routines
  2.  
  3. /*   This module will display another Dialog box
  4.      with a grahical display of the analysis.  */
  5.  
  6. #include "survdemo.h"
  7.  
  8.  /* DEFINE OWNER-DRAW BUTTON BORDER THICKNESS */
  9. #define THICKNESS   3
  10.  
  11.  
  12.  
  13.   /* GDI Handles Used Only In grphdemo.c */
  14. HBRUSH hBrushBkGndGraph;
  15. HBITMAP hBitmapBkGndGraph;
  16.  
  17. extern HWND hDlgGraph;
  18.  
  19. extern HICON hIconGraph;
  20.  
  21. #define MAXRECS  45
  22. #define nFields  45
  23.  
  24. // Array of stat data
  25.  
  26. extern STATDATA statdata[MAXRECS];
  27.  
  28.  
  29. #define TOP      1
  30. #define BOTTOM   2
  31.  
  32.  
  33.  
  34.  
  35. /***************************************************/
  36. BOOL FAR PASCAL GraphDlgProc( HWND hDlg,
  37.                     unsigned iMessage, WORD wParam,
  38.                     LONG lParam )
  39.   {
  40.   static HWND hCtrlGraph11;
  41.   static HWND hCtrlGraph12;
  42.   static HWND hCtrlGraph13;
  43.   static HWND hCtrlGraph14;
  44.   static HWND hCtrlGraph15;
  45.   static HWND hCtrlGraph16;
  46.   static HWND hCtrlGraph17;
  47.   static HWND hCtrlGraph18;
  48.   static HWND hCtrlGraph19;
  49.   static HWND hCtrlQuit;
  50.   static HWND hCtrlUpdate;
  51.  
  52.  
  53.   WORD CtrlID;   /* ID of DlgBox's child control */
  54.   int type, id;
  55.  
  56.     /* Used in Scrollbar and Push Button Movement */
  57.  
  58.   static short nVscrollPos,nVscrollMax,nVscrollInc;
  59.   int moveButtons;
  60.   RECT rect;
  61.   static RECT rectQuitTop,rectUpdateTop,
  62.                    rectQuitBottom,rectUpdateBottom;
  63.   static RECT rectClientQuit,rectClientUpdate;
  64.   POINT point;
  65.   static int buttonMove;
  66.  
  67.   switch ( iMessage )
  68.     {
  69.     case WM_INITDIALOG:
  70.  
  71.       hBitmapBkGndGraph  = LoadBitmap(hInst,
  72.                                        "BkGndCnY");
  73.       hBrushBkGndGraph = CreatePatternBrush
  74.                                (hBitmapBkGndGraph);
  75.             // Handles of Controls to be Painted
  76.       hCtrlGraph11 = GetDlgItem(hDlg, IDG_GRAPH11);
  77.       hCtrlGraph12 = GetDlgItem(hDlg, IDG_GRAPH12);
  78.       hCtrlGraph13 = GetDlgItem(hDlg, IDG_GRAPH13);
  79.       hCtrlGraph14 = GetDlgItem(hDlg, IDG_GRAPH14);
  80.       hCtrlGraph15 = GetDlgItem(hDlg, IDG_GRAPH15);
  81.       hCtrlGraph16 = GetDlgItem(hDlg, IDG_GRAPH16);
  82.       hCtrlGraph17 = GetDlgItem(hDlg, IDG_GRAPH17);
  83.       hCtrlGraph18 = GetDlgItem(hDlg, IDG_GRAPH18);
  84.       hCtrlGraph19 = GetDlgItem(hDlg, IDG_GRAPH19);
  85.       hCtrlQuit    = GetDlgItem(hDlg, IDGPB_QUIT11);
  86.       hCtrlUpdate  = GetDlgItem(hDlg, IDGPB_UPDATE11);
  87.  
  88.       /* Window Scrollbar Data */
  89.       GetClientRect(hDlg,&rect);
  90.       nVscrollMax= ((rect.bottom - rect.top)*.53/2)*2;
  91.  
  92.       nVscrollPos = 0;
  93.       SetScrollRange (hDlg,SB_VERT,0,nVscrollMax,
  94.                                               FALSE);
  95.       SetScrollPos   (hDlg,SB_VERT,nVscrollPos,TRUE);
  96.  
  97.  /* Data used in moving Quit and Update Buttons
  98.                                    with Scrollbar */
  99.       GetClientRect(hCtrlQuit,&rectClientQuit);
  100.       buttonMove = rect.bottom-rectClientQuit.bottom;
  101.       GetWindowRect(hCtrlQuit,&rectQuitTop);
  102.       point.x=rectQuitTop.left;
  103.       point.y=rectQuitTop.top;
  104.       ScreenToClient(hDlg,&point);
  105.       rectQuitTop.left=rectQuitBottom.left=point.x;
  106.       rectQuitTop.top=point.y;
  107.       buttonMove -= (point.y+3);
  108.       rectQuitBottom.top=point.y+buttonMove;
  109.  
  110.       GetClientRect(hCtrlUpdate,&rectClientUpdate);
  111.       buttonMove = rect.bottom-rectClientUpdate.
  112.                                               bottom;
  113.       GetWindowRect(hCtrlUpdate,&rectUpdateTop);
  114.       point.x=rectUpdateTop.left;
  115.       point.y=rectUpdateTop.top;
  116.       ScreenToClient(hDlg,&point);
  117.       rectUpdateTop.left=rectUpdateBottom.left=
  118.                                             point.x;
  119.       rectUpdateTop.top=point.y;
  120.       buttonMove -= (point.y+3);
  121.       rectUpdateBottom.top=point.y+buttonMove;
  122.  
  123.       break;
  124.  
  125.       /* Owner-Draw Buttons */
  126.  
  127.     case WM_DRAWITEM:
  128.       id = ((LPDRAWITEMSTRUCT)lParam)->CtlID;
  129.       switch (id)
  130.         {
  131.         case IDGPB_UPDATE11:
  132.           SetODButton((LPDRAWITEMSTRUCT)lParam,0,0,
  133.                                 DT_CENTER,THICKNESS);
  134.           return TRUE;
  135.  
  136.         case IDGPB_QUIT11:
  137.           SetODButton((LPDRAWITEMSTRUCT)lParam,0,0,
  138.                                 DT_CENTER,THICKNESS);
  139.           return TRUE;
  140.  
  141.         default:
  142.           break;
  143.         }
  144.       break;
  145.  
  146.  
  147.     case WM_PAINT:
  148.       if (IsIconic(hDlg))
  149.         {
  150.         PaintIcon(hDlg,hIconGraph);
  151.         break;
  152.         }
  153.       else
  154.         {
  155.         PaintGraphDlgCtrl(hCtrlGraph11, 0);
  156.         PaintGraphDlgCtrl(hCtrlGraph12, 5);
  157.         PaintGraphDlgCtrl(hCtrlGraph13, 10);
  158.         PaintGraphDlgCtrl(hCtrlGraph14, 15);
  159.         PaintGraphDlgCtrl(hCtrlGraph15, 20);
  160.         PaintGraphDlgCtrl(hCtrlGraph16, 25);
  161.         PaintGraphDlgCtrl(hCtrlGraph17, 30);
  162.         PaintGraphDlgCtrl(hCtrlGraph18, 35);
  163.         PaintGraphDlgCtrl(hCtrlGraph19, 40);
  164.         break;
  165.         }
  166.  
  167.         /* Parse WM_CTLCOLOR here */
  168.  
  169.     case WM_CTLCOLOR:
  170.       type = HIWORD (lParam);
  171.       CtrlID = GetWindowWord (LOWORD (lParam),
  172.                                            GWW_ID);
  173.  
  174.       switch (type)                                       {
  175.         case CTLCOLOR_DLG:
  176.           SetTextColor(wParam,RED);
  177.           return ((DWORD) hBrushBkGndGraph);
  178.  
  179.         case CTLCOLOR_BTN:
  180.           SetBkMode(wParam, TRANSPARENT);
  181.           SetBkColor(wParam, GetSysColor
  182.                                     (COLOR_WINDOW));
  183.           switch(CtrlID)
  184.             {
  185.             case IDGPB_UPDATE11:
  186.               SelectObject(wParam,hDarkGrayPen);
  187.               SetTextColor(wParam,BLUE);
  188.               return ((DWORD) hBrushYellow);
  189.  
  190.             case IDGPB_QUIT11:
  191.               SelectObject(wParam,hDarkGrayPen);
  192.               SetTextColor(wParam,BLUE);
  193.               return ((DWORD) hBrushRed);
  194.  
  195.             default:
  196.               SetTextColor(wParam, DARKGREEN);
  197.               SetBkMode(wParam, TRANSPARENT);
  198.               return ((DWORD) hBrushBkGndGraph);
  199.             }
  200.  
  201.         case CTLCOLOR_STATIC:
  202.           SetBkMode(wParam, TRANSPARENT);
  203.           switch(CtrlID)
  204.             {
  205.             case IDG_CAPTION11:
  206.               SetTextColor(wParam,RED);
  207.               SelectObject(wParam,hFontTimesSmall);
  208.               return ((DWORD) hBrushBkGndGraph);
  209.  
  210.             case IDG_GRAPH11:
  211.               SetTextColor(wParam,BLUE);
  212.               return ((DWORD) hBrushCyan);
  213.  
  214.             case IDG_GRAPH12:
  215.               SetTextColor(wParam,BLUE);
  216.               return ((DWORD) hBrushYellow);
  217.  
  218.             case IDG_GRAPH13:
  219.               SetTextColor(wParam,BLUE);
  220.               return ((DWORD) hBrushMagenta);
  221.  
  222.             case IDG_GRAPH14:
  223.               SetTextColor(wParam,BLUE);
  224.               return ((DWORD) hBrushLightBlue);
  225.  
  226.             case IDG_GRAPH15:
  227.               SetTextColor(wParam,BLUE);
  228.               return ((DWORD) hBrushGreen);
  229.  
  230.             case IDG_GRAPH16:
  231.               SetTextColor(wParam,BLUE);
  232.               return ((DWORD) hBrushRed);
  233.  
  234.             case IDG_GRAPH17:
  235.               SetTextColor(wParam,BLUE);
  236.               return ((DWORD) hBrushLightMagenta);
  237.  
  238.             case IDG_GRAPH18:
  239.               SetTextColor(wParam,BLUE);
  240.               return ((DWORD) hBrushDarkYellow);
  241.  
  242.             case IDG_GRAPH19:
  243.               SetTextColor(wParam,BLUE);
  244.               return ((DWORD) hBrushLightRed);
  245.  
  246.             case IDG_SA11: case IDG_SD11:
  247.             case IDG_N11: case IDG_D11:
  248.             case IDG_A11:
  249.               SetTextColor(wParam,BLUE);
  250.               SelectObject(wParam,hFontTimesSmall);
  251.               return ((DWORD) hBrushBkGndGraph);
  252.  
  253.             default:
  254.               SetTextColor(wParam, DARKGREEN);
  255.               SetBkMode(wParam, TRANSPARENT);
  256.               return ((DWORD) hBrushBkGndGraph);
  257.             }
  258.  
  259.         default:
  260.  
  261.           SetTextColor(wParam, BLUE);
  262.           SetBkMode(wParam, TRANSPARENT);
  263.           return ((DWORD) hBrushBkGndGraph);
  264.         }
  265.  
  266.  /* Yes, we can scroll our dialog box
  267.          and all the controls in it.  */
  268.  
  269.     case WM_VSCROLL :
  270.       moveButtons = FALSE;
  271.       switch (wParam)
  272.         {
  273.         case SB_PAGEDOWN :
  274.           nVscrollInc += nVscrollMax ;
  275.           moveButtons = BOTTOM;
  276.           break;
  277.         case SB_LINEDOWN :
  278.           nVscrollInc += 16 ;
  279.           break ;
  280.         case SB_PAGEUP :
  281.           nVscrollInc -= nVscrollMax ;
  282.           moveButtons = TOP;
  283.           break;
  284.         case SB_LINEUP :
  285.           nVscrollInc -=16;
  286.           break ;
  287.         case SB_TOP:
  288.           nVscrollInc = 0 ;
  289.           moveButtons = TOP;
  290.           break ;
  291.         case SB_BOTTOM :
  292.           nVscrollInc = nVscrollMax ;
  293.           moveButtons = BOTTOM;
  294.           break ;
  295.         case SB_THUMBPOSITION :
  296.         case SB_THUMBTRACK :
  297.           nVscrollInc= LOWORD (lParam)- nVscrollPos ;
  298.           break ;
  299.         default :
  300.           nVscrollInc = 0 ;
  301.         }
  302.       nVscrollInc = (nVscrollInc/2)*2;
  303.       if (nVscrollInc = max(-nVscrollPos,
  304.                       min(nVscrollInc,nVscrollMax -
  305.                        nVscrollPos)))
  306.         {
  307.         nVscrollPos += nVscrollInc;
  308.         SetScrollPos (hDlg,SB_VERT,nVscrollPos,TRUE);
  309.         ScrollWindow(hDlg,0,-nVscrollInc,NULL,NULL);
  310.  
  311.  
  312.  /* All this stuff is to show that we can
  313.         move our buttons, or controls in general,
  314.         around in the dialog box  */
  315.  
  316.         if (nVscrollPos == 0)
  317.           moveButtons = TOP;
  318.         if (nVscrollPos == nVscrollMax)
  319.           moveButtons = BOTTOM;
  320.         if (moveButtons == TOP)
  321.           {
  322.           MoveWindow(hCtrlQuit,rectQuitTop.left,
  323.             rectQuitTop.top,rectClientQuit.right,
  324.             rectClientQuit.bottom,TRUE);
  325.           MoveWindow(hCtrlUpdate,rectUpdateTop.left,
  326.             rectUpdateTop.top,rectClientUpdate.right,
  327.             rectClientUpdate.bottom,TRUE);
  328.           }
  329.         else if (moveButtons == BOTTOM)
  330.           {
  331.           MoveWindow(hCtrlQuit,rectQuitBottom.left,
  332.             rectQuitBottom.top,rectClientQuit.right,
  333.             rectClientQuit.bottom,TRUE);
  334.           MoveWindow(hCtrlUpdate,
  335.             rectUpdateBottom.left,rectUpdateBottom.
  336.             top,rectClientUpdate.right,
  337.             rectClientUpdate.bottom,TRUE);
  338.           }
  339.         else
  340.           ;
  341.         }
  342.       break;
  343.  
  344.  
  345.     case WM_COMMAND:
  346.       switch ( wParam )
  347.         {
  348.         case IDGPB_UPDATE11:
  349.           PaintGraphDlgCtrl(hCtrlGraph11, 0);
  350.           PaintGraphDlgCtrl(hCtrlGraph12, 5);
  351.           PaintGraphDlgCtrl(hCtrlGraph13, 10);
  352.           PaintGraphDlgCtrl(hCtrlGraph14, 15);
  353.           PaintGraphDlgCtrl(hCtrlGraph15, 20);
  354.           PaintGraphDlgCtrl(hCtrlGraph16, 25);
  355.           PaintGraphDlgCtrl(hCtrlGraph17, 30);
  356.           PaintGraphDlgCtrl(hCtrlGraph18, 35);
  357.           PaintGraphDlgCtrl(hCtrlGraph19, 40);
  358.           break;
  359.  
  360.         case IDGPB_QUIT11:
  361.           SendMessage( hDlg, WMG_CLOSE11, 0, 0L );
  362.           break;
  363.  
  364.         default:
  365.           return FALSE;
  366.         }
  367.         break;
  368.  
  369.     case WMG_CLOSE11:
  370.             /* Send a WM_DESTROY message. */
  371.       DeleteObject (hBitmapBkGndGraph);
  372.       DeleteObject(hBrushBkGndGraph);
  373.       DestroyWindow( hDlg );
  374.       hDlgGraph= 0;
  375.       break;
  376.  
  377.     default:
  378.       return FALSE;
  379.     }
  380.   return FALSE;
  381. }
  382.  
  383.  
  384.  
  385. void PaintGraphDlgCtrl(HWND hCtrl, short dataOffset)
  386.   {
  387.   HDC    hDC;    /* to create child window's DC */
  388.   RECT   rect;   /* to get child window's
  389.                                      client rect */
  390.   WORD   ctrlID; /* child window's ID in RC file */
  391.   TEXTMETRIC tm;
  392.   HFONT hFontHeading;
  393.   static int xmax,ymax,xoffset,yoffset,i,z;
  394.   static int y=0;
  395.   char buf[20] = "xxx";
  396.   int q,r;
  397.  
  398. /* Find control dimensions */
  399.   ctrlID = GetWindowWord(hCtrl, GWW_ID);
  400.   GetClientRect(hCtrl, &rect);
  401.   InvalidateRect(hCtrl, &rect, TRUE);
  402.   UpdateWindow(hCtrl);
  403.   hDC = GetDC(hCtrl);
  404.  
  405.   xmax = (rect.right - rect.left)*8/10;
  406.   ymax = (rect.bottom - rect.top)*9/10;
  407.   xoffset = rect.left +(rect.right - rect.left)*1/10;
  408.   yoffset = rect.top + (rect.bottom - rect.top)*1/20;
  409.  
  410. /* Draw the coordinates */
  411.  
  412.   MoveTo (hDC,xoffset,yoffset);
  413.   LineTo (hDC,xoffset, yoffset+ymax);
  414.   LineTo (hDC,xoffset+xmax, yoffset+ymax);
  415.  
  416.   yoffset = yoffset + ymax;
  417.   for (i=1;i<5;i++)
  418.     {
  419.     MoveTo(hDC,xoffset+(i*xmax/4),yoffset);
  420.     LineTo(hDC,xoffset+(i*xmax/4),yoffset+3);
  421.     }
  422.   SetBkMode(hDC,TRANSPARENT);
  423.  
  424. /* Put in the coordinate values */
  425.  
  426.   GetTextMetrics(hDC,&tm);
  427.  
  428. /* If the width of the area between the left border
  429.    and the left edge of the control is very small
  430.    change the font to fit into the area.           */
  431.  
  432.   if((tm.tmAveCharWidth*3) >(rect.right - rect.left)
  433.                                                  /10)
  434.     {
  435.     r = ((rect.right - rect.left)/30)+2;
  436.     hFontHeading = CreateFont(8, r,0,0, 300,FALSE,
  437.        FALSE,FALSE,OEM_CHARSET,OUT_CHARACTER_PRECIS,
  438.        OUT_DEFAULT_PRECIS,PROOF_QUALITY,
  439.        VARIABLE_PITCH|FF_ROMAN,"Times");
  440.     SelectObject(hDC,hFontHeading);
  441.     SetBkMode(hDC, TRANSPARENT);
  442.  
  443.     for (i=1;i<5;i++)
  444.       {
  445.       MoveTo(hDC,xoffset-3,yoffset-(i*ymax/5));
  446.       LineTo(hDC,xoffset,yoffset-(i*ymax/5));
  447.       wsprintf((LPSTR)buf,"%1d",20*i);
  448.       TextOut(hDC,rect.left,yoffset-(ymax*i/5),
  449.                                     (LPSTR)buf,2);
  450.       }
  451.     TextOut(hDC,rect.left,yoffset-ymax,(LPSTR)"100",
  452.                                                   3);
  453.     DeleteObject(hFontHeading);
  454.     }
  455.   else
  456.     {
  457.     for (i=1;i<5;i++)
  458.       {
  459.       SelectObject(hDC,hFontTimes);
  460.       MoveTo(hDC,xoffset-3,yoffset-(i*ymax/5));
  461.       LineTo(hDC,xoffset,yoffset-(i*ymax/5));
  462.       wsprintf((LPSTR)buf,"%1d",20*i);
  463.         TextOut(hDC,xoffset-20,yoffset-(ymax*i/5),
  464.                                       (LPSTR)buf,2);
  465.       }
  466.     TextOut(hDC,xoffset-20,yoffset-ymax,
  467.                                     (LPSTR)"100",3);
  468.     }
  469.  
  470. /* Draw the graph */
  471.  
  472.   MoveTo(hDC,xoffset,yoffset);
  473.   for (i=0;i<5;i++)
  474.     {
  475.     y =  statdata[i+dataOffset].itemave * ymax / 100;
  476.     z = (int)y;
  477.     LineTo(hDC,xoffset+(i*xmax/4),yoffset-z);
  478.     Rectangle(hDC,(xoffset+(i*xmax/4))-2,
  479.                 (yoffset-z)-2,(xoffset+
  480.                 (i*xmax/4))+2,(yoffset-z)+2);
  481.     }
  482.   ReleaseDC(hCtrl, hDC);
  483.   }
  484.  
  485.  
  486.